package com.ruoyix.im.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.ruoyix.common.annotation.Log;
import com.ruoyix.common.annotation.RepeatSubmit;
import com.ruoyix.common.core.controller.BaseController;
import com.ruoyix.common.core.domain.PageQuery;
import com.ruoyix.common.core.domain.R;
import com.ruoyix.common.core.page.TableDataInfo;
import com.ruoyix.common.core.validate.AddGroup;
import com.ruoyix.common.core.validate.EditGroup;
import com.ruoyix.common.enums.BusinessType;
import com.ruoyix.common.utils.poi.ExcelUtil;
import com.ruoyix.im.domain.ImChatUserFriend;
import com.ruoyix.im.domain.bo.ImChatUserFriendBo;
import com.ruoyix.im.domain.vo.ImChatUserFriendVo;
import com.ruoyix.im.domain.vo.UserFriendVO;
import com.ruoyix.im.service.IImChatUserFriendService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 好友
 *
 * @author ruoyi
 * @date 2024-05-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/weblog/chatUserFriend")
public class ImChatUserFriendController extends BaseController {

    private final IImChatUserFriendService iImChatUserFriendService;

    /**
     * 查询好友列表
     */
//    @SaCheckPermission("weblog:chatUserFriend:list")
//    @GetMapping("/list")
//    public TableDataInfo<ImChatUserFriendVo> list(ImChatUserFriendBo bo, PageQuery pageQuery) {
//        return iImChatUserFriendService.queryPageList(bo, pageQuery);
//    }

    @SaCheckPermission("weblog:chatUserFriend:list")
    @GetMapping("/list")
    public TableDataInfo<ImChatUserFriendVo> list(Long userId) {
        return iImChatUserFriendService.selectFriendsList(userId);
    }


    /**
     * 导出好友列表
     */
    @SaCheckPermission("weblog:chatUserFriend:export")
    @Log(title = "好友", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ImChatUserFriendBo bo, HttpServletResponse response) {
        List<ImChatUserFriendVo> list = iImChatUserFriendService.queryList(bo);
        ExcelUtil.exportExcel(list, "好友", ImChatUserFriendVo.class, response);
    }

    /**
     * 获取好友详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("weblog:chatUserFriend:query")
    @GetMapping("/{id}")
    public R<ImChatUserFriendVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iImChatUserFriendService.queryById(id));
    }

    /**
     * 新增好友
     */
    @SaCheckPermission("weblog:chatUserFriend:add")
    @Log(title = "好友", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ImChatUserFriendBo bo) {
        return toAjax(iImChatUserFriendService.insertByBo(bo));
    }

    /**
     * 修改好友
     */
    @SaCheckPermission("weblog:chatUserFriend:edit")
    @Log(title = "好友", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ImChatUserFriendBo bo) {
        return toAjax(iImChatUserFriendService.updateByBo(bo));
    }

    /**
     * 删除好友
     *
     * @param ids 主键串
     */
    @SaCheckPermission("weblog:chatUserFriend:remove")
    @Log(title = "好友", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iImChatUserFriendService.deleteWithValidByIds(Arrays.asList(ids), true));
    }


    /**
     * 添加好友申请
     */
    @GetMapping("/addFriend")
    public R<Void> addFriend(@RequestParam("friendId") Long friendId, @RequestParam(value = "remark", required = false) String remark) {
        return iImChatUserFriendService.addFriend(friendId, remark);
    }



    /**
     * 查询好友
     */
    @GetMapping("/getFriend")
    public R<List<UserFriendVO>> getFriend(@RequestParam(value = "friendStatus", required = false) Long friendStatus) {
        List<UserFriendVO> userFriendVOS = iImChatUserFriendService.getUserFriendVOS(friendStatus);
        return R.ok(userFriendVOS);
    }




    /**
     * 修改好友
     * <p>
     * 朋友状态[-1:审核不通过或者删除好友，0:未审核，1:审核通过]
     */
    @GetMapping("/changeFriend")
    public R<Void> changeFriend(@RequestParam("friendId") Long friendId,
                                     @RequestParam(value = "friendStatus", required = false) Long friendStatus,
                                     @RequestParam(value = "remark", required = false) String remark) {
        return iImChatUserFriendService.changeFriend(friendId, friendStatus, remark);
    }


}
